package com.xiaotu.spider.project.pageprocessor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.util.DateUtils;
import com.xiaotu.common.util.FileUtil;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.common.util.SepratorUtil;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.spider.APageProcessor;

import net.sf.json.JSONObject;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.selector.Html;

/**
 * @类名 BeiAnMovPageProcessor
 * @日期 2017年4月5日
 * @作者 高海军
 * @功能 电影备案
 */
@Component("BeiAnMovPageProcessor")
public class BeiAnMovPageProcessor extends APageProcessor
{
	public static final long startTime = DateUtils
			.getDate("2015-01-01", DateUtils.DAY_FORMAT).getTime();
	
	public static final long endTime = DateUtils
			.getDate("2016-01-01", DateUtils.DAY_FORMAT).getTime();
	
	private static final SimpleDateFormat SDF = new SimpleDateFormat(
			"yyyy年MM月");
	
	@Override
	public void process(Page page)
	{
		if (page.getUrl().toString().contains(
				"http://dy.chinasarft.gov.cn/shanty.deploy/catalog.nsp"))
			listProcess(page);
		else
			detailProcess(page);
	}
	
	private void listProcess(Page page)
	{
		Html html = page.getHtml();
		List<String> ulList = html
				.xpath("//div[@class='cc boxcontent']/div[2]/ul").all();
		
		for (String ulStr : ulList)
		{
			Html ulObj = new Html(ulStr);
			String date = ulObj.xpath("//li[1]/a/text()").get().trim();
			date = RegexUtils.regexSearch("([0-9]{4})年([0-9]{1,2})月", date);
			long currTime;
			try
			{
				currTime = SDF.parse(date).getTime();
			}
			catch (ParseException e)
			{
				throw new SpiderException(
						SpiderExceptionCode.DataInvalideException);
			}
			
			if (currTime >= startTime && currTime < endTime)
				PageUtils.addTargetRequest(page,
						ulObj.xpath("//li[1]/a/@href").get(),
						page.getRequest().getExtras());
		}
	}
	
	private void detailProcess(Page page)
	{
		
		Html html = page.getHtml();
		String month = html.xpath("//div[@class='heading']/text()")
				.regex("[0-9]{4}年[0-9]{1,2}月").get();
		if (StringUtils.isEmpty(month))
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);
		
		List<String> headerList = html
				.xpath("//div[@class='ta1']/ul/li/span/text()").all();
		List<String> tableList = html.xpath("//div[@id^='divf']").all();
		
		for (int i = 0; i < headerList.size(); i++)
		{
			List<String> trList = new Html(tableList.get(i)).xpath("//tr")
					.all();
			for (int j = 1; j < trList.size(); j++)
			{
				Html trObj = new Html("<table>" + trList.get(j) + "</table>");
				List<Object> dataList = new ArrayList<Object>();
				dataList.add(month);
				dataList.add(headerList.get(i));
				dataList.add(trObj.xpath("//td[3]/text()").get());
				dataList.add(trObj.xpath("//td[5]/text()").get());
				
				FileUtil.writeFile("D:\\电影备案.csv",
						StringUtil.list2CSVLine(dataList,
								SepratorUtil.SEP_MARK_EN)
								+ SepratorUtil.LINE_SEPRATOR);
			}
		}
		
	}
	
	@Override
	protected void setSiteRequest()
	{
		Map<String, Object> tvMap = new HashMap<String, Object>();
		tvMap.put("tvid", "1");
		tvMap.put("tvName", "1");
		
		String[] urlArr = new String[] {
				"http://dy.chinasarft.gov.cn/shanty.deploy/catalog.nsp?id=0129dffcccb1015d402881cd29de91ec&pageIndex=5",
				"http://dy.chinasarft.gov.cn/shanty.deploy/catalog.nsp?id=0129dffcccb1015d402881cd29de91ec&pageIndex=6",
				"http://dy.chinasarft.gov.cn/shanty.deploy/catalog.nsp?id=0129dffcccb1015d402881cd29de91ec&pageIndex=7",
				"http://dy.chinasarft.gov.cn/shanty.deploy/catalog.nsp?id=0129dffcccb1015d402881cd29de91ec&pageIndex=8" };
		for (String url : urlArr)
		{
			Request request = new Request(url);
			request.setExtras(tvMap);
			this.site.addStartRequest(request);
		}
		
	}
	
	@Override
	protected String getSpiderType()
	{
		return "BeiAnMov";
	}
	
	@Override
	public String getPageDataType(Page page)
	{
		return "MOV";
	}
	
	@Override
	protected Request getStartRequest(JSONObject target)
	{
		return null;
	}
	
}
